home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 73021.1122@compuserve.com (Lee Hayden)
- Newsgroups: comp.lang.rexx
- Subject: Re: string substitution in (standard) REXX
- Date: Sat, 20 Jan 1996 15:20:00 GMT
- Organization: CompuServe Incorporated
- Distribution: inet
- Message-ID: <4dr1a6$h4c@dub-news-svc-4.compuserve.com>
- References: <4d2mp2$1up@hyperion.mfltd.co.uk> <4d2phs$kr2@news.be.innet.net>
- NNTP-Posting-Host: dd33-138.compuserve.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- michelc@jeeves.be (Michel Castelein) wrote:
-
- >Derek Morris <dom@mfltd.co.uk> wrote:
-
- >>Hi
-
- >>I want to find a QUICK way of doing a general purpose string substitution
- >>in 'standard' REXX (i.e not using a prticular vendors extensions).
- >>For example, I want to replace all 'foo' with 'bar' in every line in a file.
- >>I have tried OVERLAY having found the substring, but that is quite slow.
- >>Any better suggestions? Thanks
- >>Derek Morris
-
- >Your REXX exec can invoke the 'standard' editor (e.g. XEDIT under VM,
- >or the ISFP editor) to execute an editor command such as 'CHANGE FOO
- >BAR ALL).
- >
- >Michel _\|/_
- > >0 0<
- >------------------------oOo-(_)-oOo-------------------------
- >| Michel Castelein |
- >| Education Consultant (MVS, OS/390) |
- >| E-mail michelc@jeeves.be |
- >| Home Page: http://www.club.innet.be/~pub00543/ |
- >| |
- >| Company: JEEVES CONSULTING N.V. |
- >| Bedrijvencentrum |
- >| Mechelsesteenweg 277 |
- >| B - 1800 Vilvoorde (Belgium) |
- >| Telephone: +32-2-2516650 |
- >| Fax: +32-2-2525755 |
- >------------------------------------------------------------
-
- Michel,
-
- Here are a couple of GP string substitution routines. Hope the are
- what you are looking for.
-
- Lee Hayden
- DB2 Sysprog
-
- /* REXX */
-
- string = 'The quick brown fox jumped over the lazy dog.'
- new_string = change_string(string,'dog.','cat,')
- say 'old string=>'string'<'
- say 'new string=>'new_string'<'
- exit
-
-
- Change_string: Procedure /* function to change all */
- /* occurances of a substring */
- /* found in a larger string */
- /* to another supplied value. */
-
- parse arg string,from_s,to_s /* preserve case in parms */
- len_from=length(from_s) /* save lenght for later process */
- len_to=length(to_s)
- from_pos=1 /* initial position to begin scan */
- /* this gets incremented as we */
- /* get hits to avoid problem with */
- /* replacements strings that are */
- /* supersets of the original. */
-
- do while pos(from_s,string,from_pos)>0
- from_pos=pos(from_s,string,from_pos) /* update latest pos
- */
- string=delstr(string,from_pos,len_from) /* delete "from"
- */
- string=insert(to_s,string,from_pos-1) /* insert "to"
- */
- from_pos=from_pos+len_to /* bump past change
- */
- end
-
- return string
-
- /* REXX */
-
- string = 'The quick brown fox jumped over the lazy dog.'
- new_string = change_string(string,'dog.','cat,')
- say 'old string=>'string'<'
- say 'new string=>'new_string'<'
- exit
-
- Change_string: Procedure /* function to change all */
- /* occurances of a substring */
- /* found in a larger string */
- /* to another supplied value. */
-
- parse arg string,from_s,to_s /* preserve case in parms */
-
- whats_left=string /* set initial value */
-
- if pos(from_s,whats_left)>0 then /* found at least one */
- do
- k=0 /* index for changed "chunks" */
- do until pos(from_s,whats_left)=0 /* no more */
-
- /* split string using the "old" string as the parse
- delimiter. Then put the string back together with
- the "new" string in its place. Save this fron "chunk"
- in a separate array of changed "chunks" to prevent
- loop in case where new string is superset of old.
- */
-
- parse var whats_left first_part (from_s) whats_left
- k=k+1
- changed.k=first_part]]to_s /* make change */
- end
-
- temp='' /* concatenate changed "chunks" */
- do l=1 to k
- temp=temp]]changed.l
- end
- temp=temp]]whats_left /* add whats left */
-
- end /* "found" block if/do */
-
- return temp
-
-